package com.warrior.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.warrior.configer.ConfigHelper;
import com.warrior.entity.ArticleEntity;
import com.warrior.service.ArticleService;
import com.warrior.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * 后台文章管理controller
 * Created by shawn on 2017/4/24.
 */
@Controller
@RequestMapping("/admin")
public class AdminArticleController {


    @Autowired
    private ArticleService articleService;

    /**
     * 文章管理列表
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/Article_list")
    public ModelAndView articleList(HttpServletRequest request, HttpServletResponse response,String size,String page,String orderType){
        ModelAndView mv = new CModelAndView("admin/article_list",request,response);
        Page<ArticleEntity> articleEntities = this.articleService.findAllArticle(CommonUtils.String2Int(size), CommonUtils.String2Int(page), orderType);
        mv.addObject("articleEntities",articleEntities);
        return mv;
    }


    /**
     * 文章路由
     * @param request request
     * @param response response
     * @return ModelAndView
     */
    @RequestMapping(value = "/Article")
    public ModelAndView saveArticle(HttpServletRequest request, HttpServletResponse response,String articleId){
        ModelAndView mv = new CModelAndView("/admin/article_id",request,response);
        ArticleEntity articleEntity = this.articleService.findArticleById(articleId);
        mv.addObject("articleEntity",articleEntity);
        return mv;
    }

    /**
     * 保存文章
     * @param request request
     * @param title 记录文章标题字段
     * @param articleShortContent 记录文章的简介
     * @param content 记录文章内容
     * @param status 记录文章状态 0:未启用 1:启用
     * @param articleDescription 记录文章描述信息
     * @param articlePath 记录文章位置
     * @return
     */
    @RequestMapping(value = "/save_article")
    @ResponseBody
    public String saveArticleToServer(String articleId,HttpServletRequest request,String title,String articleShortContent,String content,String status,String articleDescription,String articlePath){
        ArticleEntity articleEntity = new ArticleEntity();
        if (null!=articleId && !"".equals(articleId)){
            articleEntity=this.articleService.findArticleById(articleId);
        }else{
            articleEntity.setId("AR-"+ UUID.randomUUID().toString());
        }
        articleEntity.setTitle(title);
        articleEntity.setUpdateBy("admin");
        articleEntity.setCreatedBy("admin");
        DateUtil dateUtil = new DateUtil();
        articleEntity.setUpdateAt(dateUtil.Date2Timestamp(new Date()));
        articleEntity.setCreatedAt(dateUtil.Date2Timestamp(new Date()));
        articleEntity.setArticleShortContent(articleShortContent);
        articleEntity.setContent(content);
        articleEntity.setStatus(CommonUtils.String2Int(status));
        // 图片上传开始
        String uploadFilePath = ConfigHelper.getConfig().getString("upload.path");
        String saveFilePathName = request.getSession().getServletContext()
                .getRealPath("/") + uploadFilePath + "/User_photo";
        Map map;
        String fileName = "";
        try {
            map = UploadUtils.saveFileToServer(request, "photo", saveFilePathName,
                    fileName, null);
            if (fileName.equals("")) {
                if (map.get("fileName") != "") {
                    fileName = (String) map.get("fileName");
                    String savePath = "/upload/User_photo/" + fileName;
                    articleEntity.setArticlePic(savePath);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        articleEntity.setArticlePath(articlePath);
        articleEntity.setArticleDescription(articleDescription);
        this.articleService.saveArticle(articleEntity);
        String returnMap = null;
        try {
            returnMap= ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }


    @RequestMapping(value = "/delete_article",method = RequestMethod.DELETE)
    @ResponseBody
    public String deleteArticle(String articleId){
        this.articleService.deleteArticle(articleId);
        String returnMap = null;
        try {
            returnMap= ServerResponse.okToJsonString("success");
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return returnMap;
    }




}
